{include file="header.html"}
<body class="gr">

<div class="navBar">
	<a class="left" href="javascript:history.back()"><i class="return"></i></a>
	<div class="titleView-x">服务地址</div>
	<a class="right" href="javascript:void(0)">保存</a>
</div>

<form action="api.php?app=address&act=edit" method="post">
<div class="main-top address-get">
	<a class="showArea" href="javascript:void(0)">
		<i></i>
		<span>{if $data->id>0}{$data->address}{/if}</span>
	</a>
	<font>楼层和门牌号</font>
	<span><input type="text" name="room_number" id="room_number" placeholder="请输入楼层和门牌号" value="{if $data->id>0}{$data->room_number}{/if}" /></span>
</div>
<input type="hidden" name="gourl" value="wap.php?app=address&act=index" />
<input type="hidden" name="id" value="{$data->id}" />
<input type="hidden" name="province" id="province" value="{if $data->id>0}{$data->province}{/if}" />
<input type="hidden" name="city" id="city" value="{if $data->id>0}{$data->city}{/if}" />
<input type="hidden" name="district" id="district" value="{if $data->id>0}{$data->district}{/if}" />
<input type="hidden" name="address" id="address" value="{if $data->id>0}{$data->address}{/if}" />
</form>

{include file="footer.html"}
<script>
$(function(){
	{if $data->id==0}
	$.getBaiduNearby(function(xq, json){
		$('#province').val(json.result.addressComponent.province);
		$('#city').val(json.result.addressComponent.city);
		$('#district').val(json.result.addressComponent.district);
		var address = json.result.addressComponent.street+json.result.addressComponent.street_number;
		$('#address').val(address);
		$('.address-get a span').html(address);
	});
	{/if}
	$('.navBar .right').click(function(){
		if (!$('#province').val().length || !$('#city').val().length || !$('#district').val().length || !$('#address').val().length) {
			alert('请选择区域');
			return;
		}
		if (!$('#room_number').val().length) {
			//alert('请输入楼层和门牌号');
			//return;
		}
		$('form').submit();
	});
	$('.address-get a').click(function(){
		showArea(function(){
			$('.faceView .address-area input').onkey({
				callback : function(code){
					if(code!=13)return;
					$.overload(null);
					$.getBaiduPlace(this.val(), '广州', function(xq, json){
						var html = '', results = json.results;
						for(var i=0; i<results.length; i++){
							html += '<li class="ge-bottom">\
										<a href="javascript:void(0)" lat="'+results[i].location.lat+'" lng="'+results[i].location.lng+'" name="'+results[i].name+'" address="'+results[i].address+'">\
											<div>'+results[i].name+'</div>\
											<span>'+results[i].address+'</span>\
										</a>\
									</li>';
						}
						$('.faceView .address-area ul').html(html);
						$.overload(false);
						$('.faceView .address-area ul a').click(function(){
							var _t = $(this);
							$.getBaiduGeocoder(_t.attr('lat'), _t.attr('lng'), function(result){
								$('#province').val(result.addressComponent.province);
								$('#city').val(result.addressComponent.city);
								$('#district').val(result.addressComponent.district);
								var address = _t.attr('address')+_t.attr('name');
								$('#address').val(address);
								$('.address-get a span').html(address);
								removeFaceView();
							});
						});
					});
				}
			});
		});
	});
});

function bindFaceView(faceView){
	var titleView = $('.faceView .navBar .titleView');
	titleView.css('margin-left', -titleView.width()/2);
	$('.faceView .navBar .left').click(function(){
		removeFaceView(faceView);
	});
	$('.faceView').each(function(){
		var height = $.window().height;
		if($(this).outerHeight(true)<height)$(this).height(height);
	});
}
function removeFaceView(faceView){
	if(typeof faceView == 'undefined')faceView = '.faceView';
	$(faceView).removeClass('x');
	setTimeout(function(){ $(faceView).remove() }, 400);
}

function showArea(fn){
	var html = '<div class="faceView gr">\
					<div class="navBar">\
						<a class="left" href="javascript:void(0)"><i class="return"></i></a>\
						<div class="titleView-x">搜索地址</div>\
					</div>\
					<div class="main-top address-area">\
						<div class="ge-bottom">\
							<i></i>\
							<input type="text" id="address" placeholder="请输入小区名或大厦名" />\
						</div>\
						<ul></ul>\
					</div>\
				</div>';
	$('body').append(html);
	setTimeout(function(){ $('.faceView').addClass('x') }, 10);
	bindFaceView();
	fn();
}
</script>